package com.codeczx.myretrofitdemo.retrofit;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;

import java.util.concurrent.Executor;

/**
 * Created by codeczx on 2018/3/7.
 */

class Platform {
    private static final String TAG = "Platform";
    private static final Platform PLATFORM = findPlatform();

    static Platform get() {
        return PLATFORM;
    }

    private static Platform findPlatform() {
        try {
            // 装载类
            Class.forName("android.os.Build");
            if (Build.VERSION.SDK_INT != 0) {
                return new Android();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Executor defaultCallbackExecutor() {
        return null;
    }

    public CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
        return null;
    }


    static class Android extends Platform {
        public Executor defaultCallbackExecutor() {
            return new MainThreadExecutor();
        }

        @Override
        public CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
            return new ExecutorCallAdapterFactory(callbackExecutor);
        }

        static class MainThreadExecutor implements Executor {

            private final Handler handler = new Handler(Looper.getMainLooper());

            @Override
            public void execute(@NonNull Runnable command) {
                handler.post(command);
            }
        }
    }
}
